import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains


# 跑通第一条用例代码
driver=webdriver.Edge()
driver.maximize_window()
driver.get('https://www.baidu.com/')
# 隐式等待
driver.implicitly_wait(1)

# 点击登录  高阶定位方法   鼠标操作
sbcz=driver.find_element(By.XPATH,'//input[@id="s-top-loginbtn" or @name="tj_login"]')
# 强制等待
time.sleep(5)
ActionChains(driver).click(sbcz).perform()
# 输入手机号    键盘操作
a=driver.find_element(By.XPATH,'//*[@id="TANGRAM__PSP_11__userName"]')
a.send_keys("13103053968")
a.send_keys(Keys.ENTER)
time.sleep(5)
# 输入密码   ID定位方法
driver.find_element(By.ID,'TANGRAM__PSP_11__password').send_keys("baidu123aaa")
time.sleep(5)
# 勾选协议  4、XPATH定位方法
driver.find_element(By.XPATH,'//*[@id="TANGRAM__PSP_11__isAgree"]').click()
time.sleep(3)
# 点击登录按钮  显示等待
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[@id="TANGRAM__PSP_11__submit"]'))).click()
# driver.find_element(By.XPATH,'//*[@id="TANGRAM__PSP_11__submit"]').click()
# time.sleep(5)
# 切换窗口
driver.switch_to.window(driver.window_handles[-1])
# 点击贴吧
driver.find_element(By.XPATH,'//a[@href="http://tieba.baidu.com/"]').click()
time.sleep(5)
# 输入框输入   高阶定位
driver.find_element(By.XPATH,'//input[@id,"wd1"]').send_keys("周杰伦")
time.sleep(5)
# 搜索按钮  进入贴吧
driver.find_element(By.XPATH,'//*[@id="tb_header_search_form"]/span[1]/a').click()
time.sleep(5)
# JS/滑动操作
driver.execute_script('window srcollTo(0,500)')
# 点击吧主推荐    5、CLASS_NAME定位方法
driver.find_element(By.CLASS_NAME,'j_nav_local_tab_link j_tbnav_tab_a').click()
time.sleep(5)
# 浏览器事件
driver.refresh()